package com.wngz;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wngz.entity.User;
import com.wngz.mapper.UserMapper;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.List;

@SpringBootTest
public class UserMapperTest05 {

    @Autowired
    private UserMapper userMapper;


    @Test
    public void demo01(){
        //在修改前要先获取数据，假设有两个用户同时更新同一条数据
        User user1 = userMapper.selectById(4L);  //version=0
        User user2 = userMapper.selectById(4L);  //version=0
        //其中一个用户修改了年龄
        user1.setAge(20);
        //执行更新操作
        userMapper.updateById(user1);   //version=1
        //另一个用户也更新年龄
        user2.setAge(22);
        userMapper.updateById(user2);   //version=0 更新失败
    }


    @Test
    public void demo02(){
        User user = new User();
        user.setName("拜月教主");
        user.setPassword("12587");
        user.setAge(40);
        user.setTel("15326421562");

        //返回影响的行数
        int row = userMapper.insert(user);
        //获取新插入的主键值
        System.out.println("主键是：" + user.getId() + "，添加了" + row + "条记录");
    }


    @Test
    public void demo03(){
        User user = new User();
        user.setId(15L);
        user.setName("酒剑仙");
        user.setPassword("102346");
        user.setAge(35);
        user.setTel("1532642");

        //返回影响的行数
        int row = userMapper.updateById(user);
        //获取新插入的主键值
        System.out.println("主键是：" + user.getId() + "，添加了" + row + "条记录");
    }

}
